package work.lianwei;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.text.csv.CsvUtil;
import cn.hutool.core.text.csv.CsvWriter;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import lombok.Builder;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.StringWriter;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

public class CSVTest {
    public static void main(String[] args) {
        List<User> data = buildData();
        System.out.println(data);
        StringWriter writer = StrUtil.getWriter();
        CsvWriter csvWriter = CsvUtil.getWriter(writer);
        csvWriter.writeBeans(data);
        System.out.println("<<<<<<<<<>>>>>>>>>>>>>");
        System.out.println(writer.toString());
        FileUtil.writeString(writer.toString(), "C:\\work-records\\c陈锡忠\\s-cdc记录推送\\test.csv", Charset.defaultCharset());
    }

    private static List<User> buildData() {
        List<User> data = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            User user = User.builder().name("name" + i).address(Address.builder().city("city" + i).province("province" + i).build()).build();
            data.add(user);
        }
        return data;
    }

    @Builder
    static class User {
        private String name;
        private Address address;



        public void setName(String name) {
            this.name = name;
        }

        public Address getAddress() {
            return address;
        }

        public void setAddress(Address address) {
            this.address = address;
        }

        public String getName() {
            return name;
        }
    }

    @Builder
    static class Address {
        private String province;
        private String city;

        public String getProvince() {
            return province;
        }

        public void setProvince(String province) {
            this.province = province;
        }

        public String getCity() {
            return city;
        }

        public void setCity(String city) {
            this.city = city;
        }

        @Override
        public String toString() {
            return JSONUtil.toJsonStr(this);
        }
    }
}
